package com.tz.service;

import com.tz.actions.Page;
import com.tz.bean.Carousel;
import com.tz.dao.ProductDao;
import com.tz.entity.Product;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 16-7-28.
 */
@Service
@Transactional
public class ProductServiceImpl implements ProductService {

    //FEATURED_COUNT 记录条数
    private static final int FEATURED_COUNT = 16;

    @Autowired
    private ProductDao productDao;

    @Override
    public List<Carousel> getCarouselList() {
        List<Carousel> carouselList = new ArrayList<>();
        //实例化 DetachedCriteria
        DetachedCriteria dc = DetachedCriteria.forClass(Product.class)
                .add(Restrictions.eq("type", "Featured")) //查询 Featured products
                .addOrder(Order.desc("id")); //默认根据id倒序排序
        //调用dao接口
        List<Product> productList = productDao.findByDetachedCriteria(dc, FEATURED_COUNT);
        //整理为 Carousel
        int mod = 4;
        int count = 0;
        Carousel carousel = new Carousel();
        for (Product product : productList) {
            carousel.getProductList().add(product);
            count++;
            if (count % mod == 0) {
                carouselList.add(carousel);
                carousel = new Carousel();
            }
        }
        return carouselList;
    }

    @Override
    public List<Product> getLatest() {
        return null;
    }

    @Override
    public List<Product> getSidebarProducts() {
        return null;
    }

    @Override
    public Page<Product> search(String searchType, String categoryId, int pageNumber, String sortBy, String sortOrder, String viewType) {
        return null;
    }
}
